// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-12

package kind

// OrderState
// 订单状态.
type OrderState int

const (
	OrderStateUndefined OrderState = iota

	OrderStateNot        // 未支付.
	OrderStateFailed     // 支付失败.
	OrderStateWaitNotify // 已支付/等待结果通知(如微信).
	OrderStateSucceed    // 支付成功.
	OrderStateRefunding  // 退单中.
	OrderStateClosed     // 已关闭.
)

var (
	orderStateTexts = map[OrderState]string{
		OrderStateSucceed:    "已支付",
		OrderStateNot:        "待支付",
		OrderStateWaitNotify: "支付中",
		OrderStateRefunding:  "退单中",
		OrderStateFailed:     "已失败",
		OrderStateClosed:     "已关闭",
	}

	orderStateFlags = map[OrderState]string{
		OrderStateNot:        "not",
		OrderStateFailed:     "failed",
		OrderStateWaitNotify: "notify",
		OrderStateSucceed:    "succeed",
		OrderStateRefunding:  "processing",
		OrderStateClosed:     "closed",
	}
)

func OrderStateDict() []map[string]any {
	var res = make([]map[string]any, 0)
	for _, v := range []OrderState{
		OrderStateSucceed,
		OrderStateNot,
		OrderStateFailed,
		OrderStateWaitNotify,
		OrderStateRefunding,
		OrderStateClosed,
	} {
		res = append(res, map[string]any{
			"Key":   v,
			"Value": v.Text(),
		})
	}
	return res
}

// CanRefund
// 是否可发起退单.
func (o OrderState) CanRefund() bool { return o == OrderStateSucceed }

func (o OrderState) IsUndefined() bool  { return o == OrderStateUndefined }
func (o OrderState) IsNot() bool        { return o == OrderStateNot }
func (o OrderState) IsFailed() bool     { return o == OrderStateFailed }
func (o OrderState) IsWaitNotify() bool { return o == OrderStateWaitNotify }
func (o OrderState) IsSucceed() bool    { return o == OrderStateSucceed }
func (o OrderState) IsClosed() bool     { return o == OrderStateClosed }
func (o OrderState) IsCancelled() bool  { return false }

func (o OrderState) Flag() string {
	if str, ok := orderStateFlags[o]; ok {
		return str
	}
	return ""
}

func (o OrderState) Text() string {
	if str, ok := orderStateTexts[o]; ok {
		return str
	}
	return ""
}
